package com.metaera.app.controller;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.metaera.app.R;
import com.metaera.app.controller.core.IPaginationPresenter;
import com.metaera.app.controller.view.BasicPaginationView;
import com.metaera.app.controller.view.RefreshLoadMoreView;
import com.metaera.app.controller.view.core.IPaginationView;
import com.metaera.app.controller.view.core.IStatusView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

abstract public class PaginationContract<T> {

    protected IPaginationPresenter<IPaginationView<T>, T> mPresenter;


    public void init(
            RecyclerView recyclerView,
            @NonNull IStatusView view,
            @NonNull SmartRefreshLayout refreshLayout
    ) {

        recyclerView.setAdapter(createAdapter(recyclerView));

        mPresenter = getPresenter();
        BasicPaginationView<T> paginationView = new BasicPaginationView<>(recyclerView, refreshLayout, view);
        paginationView.setEmptyLayoutResId(R.layout.view_state_layout_empty); // 空内容
        paginationView.setNotMoreFooterLayoutResId(R.layout.view_list_not_more); // 没有更多
        mPresenter.setModelAndView(paginationView);
        mPresenter.requestRefresh();

        // listener
        refreshLayout.setOnRefreshLoadMoreListener(new RefreshLoadMoreView<>(mPresenter));
    }


    public IPaginationPresenter<IPaginationView<T>, T> getPresenter() {
        if (mPresenter == null) {
            mPresenter = createPresenter();
        }

        return mPresenter;
    }


    protected abstract BaseQuickAdapter<T, BaseViewHolder> createAdapter(RecyclerView recyclerView);


    protected abstract IPaginationPresenter<IPaginationView<T>, T> createPresenter();

}
